#include "iostream"
#include<opencv2/opencv.hpp>
#include "include/Armor_detect.h"
#include "include/Buff_detect.h"

using namespace std;
using namespace cv;

void Armor_detection::Init_img_deal() //读取图片
{
    //this->raw_img = imread("C:\\Pictures\\13.jpg",IMREAD_ANYDEPTH);//以黑白图像形式读入
    this->raw_img = imread("C:\\Pictures\\armor_red.jpg");//原图形式读入
    if (raw_img.empty()) {
        cout << "Error" << endl;
        return ;
    }
    cv::namedWindow("raw_img",WINDOW_AUTOSIZE);
    cv::imshow("raw_img", this->raw_img);

    this->init_result = Red_bin_operate(raw_img);
    cv::namedWindow("red_result",WINDOW_AUTOSIZE);
    cv::resizeWindow("red_result",WINDOW_WIDTH,WINDOW_HEIGHT);
    cv::imshow("red_result",this->init_result);

//    this->threshold_contours =Find_contour(init_result);
//    cv::namedWindow("contour_detect_result",WINDOW_NORMAL);
//    cv::resizeWindow("contour_detect_result",WINDOW_WIDTH,WINDOW_HEIGHT);
//    cv::imshow("contour_detect_result",this->raw_img);
//
//    this->lamp_list = select_lamp(this->raw_img,this->threshold_contours);
//    cv::namedWindow("lamp_select_result",WINDOW_NORMAL);
//    cv::resizeWindow("lamp_select_result",WINDOW_WIDTH,WINDOW_HEIGHT);
//    cv::imshow("lamp_select_result",this->raw_img);

    cv::waitKey(0);
    cv::destroyAllWindows();
}

void Armor_detection::Init_video_deal()
{

    VideoCapture cap("D:\\Users\\luo\\实验室\\RobotMaster奇点\\比赛视频\\lbb_fan.mp4");
    //VideoCapture cap("C:\\Pictures\\bubing.mp4");
    //cap.open("//D:\\Users\\luo\\实验室\\RobotMaster奇点\\算法组\\奇点战队视觉文档\\迈德威视录制视频\\8.mp4");
    if(!cap.isOpened())
    {
        std::cout<<"Video open error!"<<std::endl;
        return;
    }
    else
    {
        std::cout<<"Video image's width = "<<cap.get(CAP_PROP_FRAME_WIDTH)<<std::endl;
        std::cout<<"Video image's height = "<<cap.get(CAP_PROP_FRAME_HEIGHT)<<std::endl;
        std::cout<<"Video image's fps = "<<cap.get(CAP_PROP_FPS)<<std::endl;
        std::cout<<"Video image's total frame_number = "<<cap.get(CAP_PROP_FRAME_COUNT)<<std::endl;
    }
    buffdistinguish buff;
    Target_Buff _Buff;
    std::vector<cv::Point2d> m_Points;
    string Owecolor = "blue";

    while(true)
    {
        //cap>>this->raw_img;
//        if(this->raw_img.empty())
//            break;
        //std::cout<<"Video image's fps = "<<cap.get(CAP_PROP_FPS)<<std::endl;
        if(!cap.read(this->raw_img))
        {
            std::cout<<"Image read error!"<<std::endl;
            return;
        }
        buff.dection(_Buff,raw_img,1,m_Points,Owecolor);

//        cv::namedWindow("raw_img",WINDOW_NORMAL);
//        cv::resizeWindow("raw_img",WINDOW_WIDTH,WINDOW_HEIGHT);
//        cv::imshow("raw_img",this->raw_img);

        //this->init_result = Red_base(raw_img);
//        this->init_result = Red_bin_operate(raw_img);
//        cv::namedWindow("red_result",WINDOW_NORMAL);
//        cv::resizeWindow("red_result",WINDOW_WIDTH,WINDOW_HEIGHT);
//        cv::imshow("red_result",this->init_result);

//        this->threshold_contours =Find_contour(init_result);
//        cv::namedWindow("contour_detect_result",WINDOW_NORMAL);
//        cv::resizeWindow("contour_detect_result",WINDOW_WIDTH,WINDOW_HEIGHT);
//        cv::imshow("contour_detect_result",this->raw_img);

//        this->lamp_list = select_lamp(this->raw_img,this->threshold_contours);
        //if(!select_lamp_(this->raw_img,this->threshold_contours))
        //continue;
//        cv::namedWindow("lamp_select_result",WINDOW_NORMAL);
//        cv::resizeWindow("lamp_select_result",WINDOW_WIDTH,WINDOW_HEIGHT);
//        cv::imshow("lamp_select_result",this->raw_img);

        //预处理+筛选条件决定此处成败
//        this->armor_list = select_armor(this->lamp_list);
        //if(!select_armor_(this->lamp_list))
        //continue;
//        cv::namedWindow("armor_detect_result",WINDOW_NORMAL);
//        cv::resizeWindow("armor_detect_result",WINDOW_WIDTH,WINDOW_HEIGHT);
//        cv::imshow("armor_detect_result",this->raw_img);


        if(cv::waitKey(1) == 27)
            break;

    }
    //cv::waitKey();
    cv::destroyAllWindows();
    cap.release();
}

int main(int argc,char **argv) {

    std::cout<<"Armor project test"<<std::endl;

    Armor_detection armor_detection;
    armor_detection.Init_video_deal();
    //armor.Init_img_deal();
    return 0;
}
